<?php
namespace App\Domain;
use App\Model\Live as Model_Live;
use App\Domain\Vip as Domain_Vip;

class Matchs {
    /* 检测信息 */
	public function getLiveuidInfo($uid) {

        $model = new Model_Live();
        
        $liveuidinfo= $model->getLiveuidInfo($uid);
        
        return $liveuidinfo;
    }
    /* 匹配 */
	public function match($uid,$anchorid,$type) {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        
        $configpri=\App\getConfigPri();
        $fee=[
            '1'=>[
                '0'=>$configpri['match_video'],
                '1'=>$configpri['match_video_vip'],
            ],
            '2'=>[
                '0'=>$configpri['match_voice'],
                '1'=>$configpri['match_voice_vip'],
            ],
        ];
        

        $domain_vip = new Domain_Vip();
        $uservip=$domain_vip->myVip($uid);
        
        $total=$fee[$type][$uservip['isvip']];
        
        $nowtime=time();
        $showid=$nowtime;
        $data=[
            'uid'=>$uid,
            'liveuid'=>$anchorid,
            'type'=>$type,
            'coin'=>$total,
            'starttime'=>$showid,
            'showid'=>$showid,
            'status'=>1,
        ];
        $model = new Model_Live();
        $result= $model->setConversation($data);
        
        /* 更新状态 */
        $model->upOnline($uid,2);
        $model->upOnline($anchorid,2);
        
        /* 流地址 */
        $stream=$uid.'_'.$showid;
        $stream_l=$anchorid.'_'.$showid;
		
		
		//普通rtmp推流播流
        // $push=PrivateKey_tx('rtmp',$stream,1);
        // $pull=tx_acc('rtmp',$stream,0); 
		/* $pull=PrivateKey_tx('http',$stream,0);  */
		
		//trtc推流播流
        $push=\App\getTxTrtcUrl($uid,$stream,1); //用户自己推
        $pull=\App\getTxTrtcUrl($anchorid,$stream,0); //给主播播放

		
        //普通rtmp推流播流
        // $push_l=PrivateKey_tx('rtmp',$stream_l,1);
        // $pull_l=tx_acc('rtmp',$stream_l,0);
        /* $pull_l=PrivateKey_tx('http',$stream_l,0); */

        //trtc推流播流
        $push_l=\App\getTxTrtcUrl($anchorid,$stream_l,1); //主播自己推
        $pull_l=\App\getTxTrtcUrl($uid,$stream_l,0); //给用户播放
        
        $data=[
            'uid'=>$uid,
            'liveuid'=>$anchorid,
            'type'=>$type,
            'total'=>$total,
            'showid'=>$showid,
            'push'=>$push,
            'pull'=>$pull,
            'push_l'=>$push_l,
            'pull_l'=>$pull_l,
        ];
        file_put_contents('/www/wwwroot/mangers.flami.live/test.txt', json_encode($data));
        //print_r($data);exit;
        $this->setIM($data);
		return $rs;

	}


    /* 发送私信 */
	public function setIM($info) {
        /* IM */
        /* 用户给主播发 */
        $userinfo=\App\getUserInfo($info['uid']);
        $ext=[
            'method'=>'call',
            'action'=>'12',
            'type'=>$info['type'],
            'showid'=>$info['showid'],
            
            'id'=>$userinfo['id'],
            'avatar'=>$userinfo['avatar'],
            'user_nickname'=>$userinfo['user_nickname'],
            'level'=>$userinfo['level'],
            'level_anchor'=>$userinfo['level_anchor'],
            
            'push'=>$info['push_l'],
            'pull'=>$info['pull_l'],
        ];

        //file_put_contents("2222.txt",json_encode($ext));
        
        #构造高级接口所需参数
        $msg_content = array();
        //创建array 所需元素
        $msg_content_elem = array(
            'MsgType' => 'TIMCustomElem',       //自定义类型
            'MsgContent' => array(
                'Data' => json_encode($ext),
                'Desc' => '',
            )
        );
        //将创建的元素$msg_content_elem, 加入array $msg_content
        array_push($msg_content, $msg_content_elem);
        
        $account_id=(string)$info['uid'];
        $receiver=(string)$info['liveuid'];
        $api=\App\getTxRestApi();

        $ret = $api->openim_send_msg_custom($account_id, $receiver, $msg_content);
        
        //file_put_contents('./setIM.txt',date('Y-m-d H:i:s').' 提交参数信息 :'.json_encode($msg_content)."\r\n",FILE_APPEND);
        //file_put_contents('./setIM.txt',date('Y-m-d H:i:s').' 提交参数信息 :'.json_encode($ret)."\r\n",FILE_APPEND);
        
        /* 主播给用户发 */
        $anchorinfo=\App\getUserInfo($info['liveuid']);
        $ext=[
            'method'=>'call',
            'action'=>'12',
            'type'=>$info['type'],
            'showid'=>$info['showid'],
            
            'id'=>$anchorinfo['id'],
            'avatar'=>$anchorinfo['avatar'],
            'user_nickname'=>$anchorinfo['user_nickname'],
            'level'=>$anchorinfo['level'],
            'level_anchor'=>$anchorinfo['level_anchor'],
            
            'push'=>$info['push'],
            'pull'=>$info['pull'],
        ];

        //file_put_contents("3333.txt",json_encode($ext));
        
        #构造高级接口所需参数
        $msg_content = array();
        //创建array 所需元素
        $msg_content_elem = array(
            'MsgType' => 'TIMCustomElem',       //自定义类型
            'MsgContent' => array(
                'Data' => json_encode($ext),
                'Desc' => '',
            )
        );
        //将创建的元素$msg_content_elem, 加入array $msg_content
        array_push($msg_content, $msg_content_elem);
        
        $account_id=(string)$info['liveuid'];
        $receiver=(string)$info['uid'];

        $ret = $api->openim_send_msg_custom($account_id, $receiver, $msg_content);
        
        //file_put_contents('./setIM.txt',date('Y-m-d H:i:s').' 提交参数信息 :'.json_encode($msg_content)."\r\n",FILE_APPEND);
        //file_put_contents('./setIM.txt',date('Y-m-d H:i:s').' 提交参数信息 :'.json_encode($ret)."\r\n",FILE_APPEND);
	}
	
}
